//
//  Me2DayAlertView.m
//  CairnStory
//  www.cairnstory.com
//
//  Created by saturna on 11. 3. 22..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Me2DayUIAlertView.h"


@implementation Me2DayUIAlertView

- (id)initWithTitle:(NSString*)title message:(NSString*)message
			 target:(id)target action:(SEL)action
	   buttonTitles:(NSString*)buttonTitles, ...
{
	if (title == nil)
	{
		self = [super initWithTitle:@"CairnStory"
							message:message
						   delegate:self
				  cancelButtonTitle:nil
				  otherButtonTitles:nil];
	}
	else
	{
		self = [super initWithTitle:title
							message:message
						   delegate:self
				  cancelButtonTitle:nil
				  otherButtonTitles:nil];
	}
	if (self == nil)
	{
		return self;
	}

	m_target = target == nil ? nil : [target retain];
	m_action = action;

	if (buttonTitles != nil) {
		[super addButtonWithTitle:buttonTitles];

		va_list buttonTitleList;
		va_start(buttonTitleList, buttonTitles);

		id aButtonTitle;
		while (aButtonTitle = va_arg(buttonTitleList, NSString*))
		{
			[super addButtonWithTitle:aButtonTitle];
		}
		va_end(buttonTitleList);
	}

	return self;
}





- (id)initConfirmWithTitle:(NSString*)title message:(NSString*)message
					target:(id)target action:(SEL)action
{
	self = [self initWithTitle:title
					   message:message
						target:target
						action:action
				  buttonTitles:@"Cancel", @"OK", nil];

	self.cancelButtonIndex = 0;
	return self;
}







- (id)initAlertWithTitle:(NSString*)title message:(NSString*)message
				  target:(id)target action:(SEL)action
{
	m_isAlertView = YES;
	return [self initWithTitle:title
					   message:message
						target:target
						action:action
				  buttonTitles:@"OK", nil];
}




- (id)attributeForKey:(NSString*)key
{
	return m_attributes == nil ? nil : [m_attributes objectForKey:key];
}
- (void)setAttribute:(id)value forKey:(NSString*)key
{
	if (m_attributes == nil) {
		m_attributes = [[NSMutableDictionary alloc] initWithCapacity:1];
	}
	[m_attributes setObject:value forKey:key];
}


- (void)dealloc {
	[m_target release];
	[m_attributes release];
	[super dealloc];
}




#pragma mark UIAlertViewDelegate

// Called when a button is clicked.
// The view will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if (m_target != nil && m_action != nil)
	{
		if (m_isAlertView)
		{
			[m_target performSelector:m_action
						   withObject:self];
		}
		else
		{
			[m_target performSelector:m_action
						   withObject:self
						   withObject:(id)buttonIndex];
		}
	}
}


@end

